package com.wltt.product.ahuthr.service;

import com.wltt.product.ahuthr.config.exception.BusinessException;
import com.wltt.product.ahuthr.enums.ImportTypeEnum;
import com.wltt.product.ahuthr.excel.HouseFundListener;
import com.wltt.product.ahuthr.executors.ImportHouseFundExecutor;
import com.wltt.product.ahuthr.executors.WorkStealingPool;
import com.wltt.product.ahuthr.mapper.ImportRecordMapper;
import com.wltt.product.ahuthr.mapper.SalaryHouseFundMapper;
import com.wltt.product.ahuthr.mapper.SalaryMapper;
import com.wltt.product.ahuthr.mapper.UserBasicMapper;
import com.wltt.product.ahuthr.model.ResultData;
import com.wltt.product.ahuthr.model.po.ImportRecordPO;
import com.wltt.product.ahuthr.model.po.SalaryHouseFundPO;
import com.wltt.product.ahuthr.service.base.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * @ClassName: SalaryHouseFundServiceImpl
 * @Description: 住房公积金 逻辑服务类<br/>
 * 不做业务校验，参数校验统一放到前置的Service中
 * @Date: 2024-05-30 14:34:21
 * @Author yanggl
 */
@Service(value = "salaryHouseFundService")
public class SalaryHouseFundServiceImpl extends BaseServiceImpl<SalaryHouseFundPO, Long> implements SalaryHouseFundService {

    @Value("${file.error.path: ./error}")
    private String errorPath;

    private SalaryHouseFundMapper salaryHouseFundMapper;

    @Resource
    private UserBasicMapper userBasicMapper;

    @Resource
    private SalaryMapper salaryMapper;

    @Resource
    private ImportRecordMapper importRecordMapper;

    //构造方法
    public SalaryHouseFundServiceImpl(SalaryHouseFundMapper salaryHouseFundMapper) {
        super(salaryHouseFundMapper);
        this.salaryHouseFundMapper = salaryHouseFundMapper;
    }

    /**
     * 导入公积金
     *
     * @param file
     * @param belongYear
     * @param currentYear
     * @return
     */
    @Override
    public ResultData importSalaryHouseFund(MultipartFile file, Integer belongYear, Integer currentYear) throws IOException {
        if (file.isEmpty()) {
            throw new BusinessException("未上传文件", HttpStatus.BAD_REQUEST);
        }
        if (null == belongYear) {
            throw new BusinessException("所属年不能为空", HttpStatus.BAD_REQUEST);
        }
        if (null == currentYear) {
            throw new BusinessException("未指定是否是当前年", HttpStatus.BAD_REQUEST);
        }

        String fileName = file.getOriginalFilename();
        if (!(fileName.endsWith(".xls") || fileName.endsWith(".xlsx"))) {
            throw new BusinessException("请上传excel文件", HttpStatus.BAD_REQUEST);
        }

        ImportRecordPO importRecordPO = new ImportRecordPO();
        importRecordPO.setImportType(ImportTypeEnum.HOUSE_FUND.getCode());
        importRecordPO.setImportDate(new Date());
        importRecordMapper.insertSelective(importRecordPO);

        File errorFile = new File(errorPath + "/" + System.currentTimeMillis() + ".xlsx");
        HouseFundListener houseFundListener = new HouseFundListener(this, userBasicMapper, salaryMapper, salaryHouseFundMapper, importRecordMapper, importRecordPO, belongYear, currentYear, errorFile);
        ImportHouseFundExecutor importHouseFundExecutor = new ImportHouseFundExecutor(file.getInputStream(), houseFundListener);
        WorkStealingPool.execute(importHouseFundExecutor);

        return ResultData.success();
    }

    /**
     * 更新公积金
     *
     * @param salaryId
     * @param salaryHouseFundPOList
     * @return
     */
    @Override
    @Transactional
    public ResultData updateSalaryHouseFund(Long salaryId, List<SalaryHouseFundPO> salaryHouseFundPOList) {
        salaryHouseFundMapper.deleteBySalaryId(salaryId);
        salaryHouseFundMapper.insertRecords(salaryHouseFundPOList);
        return ResultData.success();
    }


}